<?php 
    session_start(); 
    if (!isset($_SESSION['user'])) {
        header("Location: login.php");
        exit;
    }

    include("../service/categoryService.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title>Loại Sản Phẩm</title>
	<link rel="stylesheet" href="../css/admin-style.css" type="text/css" media="all" />
    <script type="text/javascript" src="../js/jquery-1.6.4.min.js"></script>
    <script type="text/javascript" src="../js/validatingDeletingFunction.js"></script>
    <script type="text/javascript">
        function hideMessage() {
            var ok = document.getElementById("msgOK");
            addClass(ok, 'msg-hidden');
        }

        function addClass(el, name){
            el.className += (el.className ? ' ' : '') +name;
        }
        
        function submitForm(){
            var form = document.getElementById("form");
            form.method = "post";
            form.action = "../action/deleteCategoryAction.php";
            form.submit();
        }
    </script>
</head>

<body>
<!-- Header -->
<?php include('header.php')?>
<!-- End Header -->


<?php
    /* process paging event */
    $page_num = $_GET['page_num'];
    if (!isset($page_num)) {
            $page_num = 1;
    }
    
    $row_per_page = 10;
    $keyword = $_GET['keyword'];

    if (!isset($keyword)) {
        $keyword = "";
    }

    $total = countNumOfCategoryByName($keyword);
    $total_page = ceil($total / $row_per_page);
    $start_position = ($page_num * $row_per_page) -  ($row_per_page);
    
    $actionResult = $_GET["actionResult"];
    $msg = "";
    
    /* process message */
    if(isset($actionResult)){
        if($actionResult == "ok"){
            $msg = "Sản phẩm đã được xóa thành công!";
            $class = "msg msg-ok";
        } elseif ($actionResult == "no") {
            $msg = "Không thể xóa sản phẩm";
            $class = "msg msg-error";
        } elseif ($actionResult == "cannot"){
            $msg = "Loại sản phẩm có sản phẩm con!";
            $class = "msg msg-error";
        }
    }
    
    if ($msg == '') $class = 'msg msg-ok msg-hidden';
?>
<form id="form" action="category.php" method="get">
<!-- Container -->
<div id="container">
    <div class="shell">
        <!-- Message OK -->		
        <div class="<?php echo $class; ?>" id="msgOK">
            <p><strong><?php echo $msg; ?></strong></p>
            <a href="#" class="close" onclick="hideMessage();">close</a>
        </div>
        <!-- End Message OK -->
        <br />
        <!-- Main -->
        <div id="main">
            <div class="cl">&nbsp;</div>

            <!-- Content -->
            <div id="content">

                <!-- Box -->
                <div class="box">
                    <!-- Box Head -->
                    <div class="box-head">
                        <h2 class="left">Danh Sách Loại Sản Phẩm</h2> 
                        <div class="right">
                            <label>Tìm Kiếm:</label>
                            <input type="text" class="field small-field" name="keyword" />
                        </div>
                    </div>
                    <!-- End Box Head -->	

                    <!-- Table -->
                    <div class="table" id="tblCategory">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <th width="13"></th>
                                <th>STT</th>
                                <th>Tên Loại Sản Phẩm</th>
                                <th>Sản Phẩm</th>
                                <th>Mô tả</th>
                            </tr>
                            <?php
                                $count = 1;
                                $result = getCategoriesBy($keyword, $cateID, $start_position, $row_per_page);
                                
                                while($row = ConnectionManager::getInstance()->fetchArray()) {
                            ?>
                            <tr>
                                <td>
                                <input type="checkbox" class="checkbox" value="<?php echo $row["CAT_ID"]; ?>" name="checkbox[]" id="checkbox[]"/>
                                </td>
                                <td><?php echo $count; ?></td>
                                <td><h3><a href="categoryDetail.php?catID=<?php echo $row["CAT_ID"]; ?>"><?php echo $row["CAT_NAME"]; ?></a></h3></td>
                                <td><?php echo $row["nProducts"]; ?></td>
                                <td><?php echo $row["CAT_DESCRIPTION"]; ?></td>
                            </tr>
                            <?php
                                    $count++;
                                }
                            ?>
                        </table>
                        <!-- Pagging -->
                        <div class="pagging">
                            <div class="left">Trang <?php echo(" " . $page_num . "  trong tổng số " .  $total_page . " trang ");?></div>
                            <div class="right">
                                <?php
                                    for ($i = 1; $i <= $total_page; $i++) {
                                        echo("<a href='" . "category.php?" . "&keyword=" . $keyword . "&page_num=" . $i . "'>" . $i . "</a>");
                                    }
                                ?>
                            </div>
                        </div>
                        <!-- End Pagging -->
                    </div>
                    <!-- Table -->
                </div>
                <!-- End Box -->

                <!-- Box -->
                <div class="box">
                    <!-- Box Head -->
                    <div class="box-head">
                            <h2>Thao tác</h2>
                    </div>
                    <!-- End ofBox Head -->
                    <div class="box-content">
                        <div class="buttons">
                            <input type="button" class="button" value="Xóa Loại" name="btnDelete" 
                                   onclick="if(validateDeletingFunction('tblCategory', 'checkbox[]', 'msgOk')) submitForm();"/>
                            <input type="button" class="button" value="Thêm Loại" onclick="window.location = 'categoryDetail.php';"/>
                        </div>
                    </div>
                </div>
            </div>
                <!-- End Content -->
            <div class="cl">&nbsp;</div>			
        </div>
        <!-- Main -->
    </div>
</div>
<!-- End Container -->
</form>
<!-- Footer -->
<div id="footer">
	<div class="shell">
		<span class="left">&copy; 2010 - VNWeb</span>
		<span class="right">
			Design by <a href="http://chocotemplates.com" target="_blank" title="The Sweetest CSS Templates WorldWide">VNWeb.vn</a>
		</span>
	</div>
</div>
<!-- End Footer -->
	
</body>
</html>